package javaroke.servlets;

import java.io.IOException;
import javaroke.negocio.GestionLocal;
import javaroke.excepcion.DAOExcepcion;
import javaroke.modelo.Local;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegistrarReservaServlet
 */
@WebServlet("/RegistrarLocalServlet")
public class RegistrarLocalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarLocalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int administrador;
		try{
			
			GestionLocal GL = new GestionLocal();
			/**Local local = new Local();
			
			local.setNombre(request.getParameter("txtNombreLocal"));
			local.setDescripcion(request.getParameter("txtDireccion"));
			administrador= Integer.parseInt(request.getParameter("LstAdministrador.value"));
				**/
			
			try {
								
				if( 
						(request.getParameter("txtNombreLocal") != null &&  !request.getParameter("txtNombreLocal").equals(" ")) && 
						(request.getParameter("txtDireccion") != null &&  !request.getParameter("txtDireccion").equals(" ")) &&
						(request.getParameter("txtTelefono") != null &&  !request.getParameter("txtTelefono").equals(" ")) &&
						(request.getParameter("txtEmailE") != null &&  !request.getParameter("txtEmailE").equals("")) &&
						(request.getParameter("txtEmailS") != null &&  !request.getParameter("txtEmailS").equals(""))
						)
				{
				   GL.insertar( 
					  	request.getParameter("txtNombreLocal"), 
						request.getParameter("txtDireccion"),
						request.getParameter("txtTelefono"),
						(request.getParameter("txtEmailE") + "@" +request.getParameter("txtEmailS")),
						request.getParameter("txtComentario"), 
						0 , 
						Integer.parseInt(request.getParameter("LstAdministrador"))
						 );		       				
				 } else { // if(request.getParameter("btnCancelar").equals("Cancelar") ){
			     	    PrintWriter pw = response.getWriter();
			        	pw.println("<h3>Registro cancelado.</h3>");
			//}
				 }	
				request.setAttribute("MENSAJE", "Se registro correctamente.");
				
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				request.setAttribute("MENSAJE", e1.getMessage());
			}
		
			
		} catch (IllegalArgumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			request.setAttribute("MENSAJE", "ERROR, Falta llenar campos o tipo de variable no soportada: -" + e2.getMessage() + "-");
	}
		
			RequestDispatcher rd = request.getRequestDispatcher("RegistroLocal.jsp");
			rd.forward(request, response);
		
	}

}
